<!-- 
 
  该组件,主要是解决环形进度条,可以实现动画加载的效果,
  如果没有动画效果,建议使用: https://element-plus.gitee.io/zh-CN/component/progress.html
 -->
<template>
  <div class="progress">
    <svg
      viewBox="0 0 96 96"
      class="svg-circle-progress"
      style="width: 200px; height: 200px"
    >
      <defs>
        <linearGradient x1="0%" y1="100%" x2="0%" y2="0" id="gradient">
          <stop offset="0%" stop-color="#FFD38D"></stop>
          <stop offset="50%" stop-color="#FFD38D"></stop>
          <stop offset="100%" stop-color="#00CCCF"></stop>
        </linearGradient>
      </defs>
      <circle
        r="40"
        cx="48"
        cy="48"
        fill="none"
        stroke-miterlimit="20"
        stroke-width="12"
        class="svg-progress"
        style="stroke-dasharray: 275, 279.602; stroke: #f5f5f5"
      ></circle>

      <circle
        r="40"
        cx="48"
        cy="48"
        fill="none"
        stroke-miterlimit="20"
        stroke-width="12"
        class="svg-progress"
        :stroke-dasharray="progressValue + ',' + 279.602"
        stroke="url(#gradient)"
      ></circle>
    </svg>
    <div class="mask">
      <p class="bigData">{{ showProgress }}%</p>
      <p class="text">正确率</p>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, toRefs, watch, onMounted } from "vue";

const props = defineProps({
  targetValue: {
    type: Number,
    require: true,
    default: -1,
  },
  color: {
    type: String,
    default: "#00CCCF",
  },
  width: {
    type: String,
    default: "210px",
  },
  height: {
    type: String,
    default: "200px",
  },
});
// height, width, color,
const { targetValue } = toRefs(props);

const showProgress = ref<number>(0);
const addData = () => {
  if (targetValue.value === 0) return;
  let timer = setInterval(() => {
    if (targetValue.value === showProgress.value) {
      clearInterval(timer), timer == null;
      return;
    }
    ++showProgress.value;
  }, 15);
};
onMounted(() => {
  addData();
});
const progressValue = ref<number>((showProgress.value / 100) * 250);

watch(showProgress, (newValue: number) => {
  progressValue.value = (newValue / 100) * 250;
});
</script>
<style lang="scss" scoped>
.progress {
  display: inline-block;
  position: relative;
  width: 200px;
  height: 200px;
  text-align: center;
}
.svg-circle-progress {
  position: relative;
  transform: rotate(-90deg);
}
.svg-progress {
  //   stroke: #00CCCF;
  stroke-linecap: round;
  transition: all 0.3s linear;
}
.mask {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: bold;
  .bigData {
    font-size: 30px;
    color: #232323;
    margin-right: -5px;
  }
  .text {
    color: #999;
    line-height: 22px;
  }
}
</style>
